﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2
{
    class Apple
    {
        private int x;
        private int y;

        public Apple() {
            Random ran = new Random();
            x = ran.Next(27);// for x: wide number.
            y = ran.Next(24);// for y: lenght number.
            //check for the first apple in the board:
            while ((x == 13 && y == 12) || (x == 13 && y == 13) || (x == 13 && y == 14))//check the initial possitions of the snake
            {
                x = ran.Next(27);// for x: wide number.
                y = ran.Next(24);// for y: lenght number.
            }

        }

        public Apple(LinkedList<SnakePart> snake)//constructor taking count of the snake position
        {
            Random ran = new Random();
            x = ran.Next(27);// for x: wide number.
            y = ran.Next(24);// for y: lenght number.
            //check for the first apple in the board:
            /*while ((x == 13 && y == 12) || (x == 13 && y == 13) || (x == 13 && y == 14))//check the initial possitions of the snake
            {
                x = ran.Next(27);// for x: wide number.
                y = ran.Next(24);// for y: lenght number.
            }*/

            LinkedListNode<SnakePart> node = snake.First; //access to the data List
            while (node != null)
            {
                if (node.Value.getX() == x && node.Value.getY() == y)
                {
                    x = ran.Next(27);// for x: wide number.
                    y = ran.Next(24);// for y: lenght number.
                    node = snake.First;
                }
                else node = node.Next;
            }

            System.Console.Write("APPLE : "+x+" "+y);


        }

        ///Retuns in the location where we wants the apple
        public int[] location() { 
            int[] location = new int[2];
            location[0]= this.x * 20; // * size of the apple
            location[1] = this.y * 20;
            return location;
        }

        public int getX()
        {
            return this.x;
        }

        public int getY()
        {
            return this.y;
        }

        public void generateNewApple(LinkedList<SnakePart> snake)
        {
            Random ran = new Random();
            x = ran.Next(27);// for x: wide number.
            y = ran.Next(24);// for y: lenght number.

            LinkedListNode<SnakePart> node = snake.First; //access to the data List
            while (node != null)
            {
                if (node.Value.getX() == x && node.Value.getY() == y)
                {
                    x = ran.Next(27);// for x: wide number.
                    y = ran.Next(24);// for y: lenght number.
                    node = snake.First;
                }
                else node = node.Next;
            }
        }
    }
}
